﻿Imports System.Drawing.Drawing2D
Imports System.Drawing
Imports System.Windows.Forms
Imports ajayumi.develop.VBasic.Controls.Forms.Utilities
Imports ajayumi.develop.VBasic.Controls.Forms.Renders
Imports ajayumi.develop.VBasic.Controls.Forms.Events
Imports ajayumi.develop.VBasic.Controls.Forms.Themes
Imports ajayumi.develop.VBasic.Controls.Forms.Themes.default

Namespace Forms.Renders
    Public Class FlatRectangleRender
        Inherits FormRenderBase

#Region "Variables"
        Private m_Theme As ThemeBase = Nothing
#End Region

#Region "Properties"

        Public ReadOnly Property Theme() As ThemeBase
            Get
                If Me.m_Theme Is Nothing Then
                    Me.m_Theme = New DefaultTheme()
                End If
                Return Me.m_Theme
            End Get
        End Property

        Public Overrides ReadOnly Property Factor() As Integer
            Get
                Return 5
            End Get
        End Property

#End Region

#Region "Constructors"
        Public Sub New()
            MyBase.New()

        End Sub
        Public Sub New(ByVal theme As ThemeBase)
            Me.m_Theme = theme
        End Sub
#End Region

#Region "Methods"
        '[Private]－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－
        Private Overloads Sub drawBorder(ByVal g As Graphics, ByVal rect As Rectangle, ByVal diagonal As Integer, ByVal style As CornerStyle)
            rect.Width -= 1
            rect.Height -= 1
            Using _GraphicsPath As GraphicsPath = GraphicsPathHelper.CreateFlatRectanglePath(rect, diagonal, style)
                Using _Pen As New Pen(Me.Theme.Border)
                    g.DrawPath(_Pen, _GraphicsPath)
                End Using
            End Using

            rect.Inflate(-1, -1)
            Using _GraphicsPath As GraphicsPath = GraphicsPathHelper.CreateFlatRectanglePath(rect, diagonal, style)
                Using _Pen As New Pen(Me.Theme.InnerBorder)
                    g.DrawPath(_Pen, _GraphicsPath)
                End Using
            End Using
        End Sub

#Region "Draw"
        ''' <summary>
        ''' 绘制标题栏背景
        ''' </summary>
        ''' <param name="g"></param>
        ''' <param name="captionRect"></param>
        ''' <param name="active"></param>
        ''' <remarks></remarks>
        Protected Overridable Sub DrawCaptionBackground(ByVal g As Graphics, ByVal captionRect As Rectangle, ByVal active As Boolean)
            Dim _Color As Color = CType(IIf(active, Me.Theme.CaptionActive, Me.Theme.CaptionDeactive), Color)
            Using _OptimizeGraphics As New OptimizeGraphics(g)
                Using _Brush As New SolidBrush(_Color)
                    Using _GraphicsPath As GraphicsPath = GraphicsPathHelper.CreateFlatRectanglePath(captionRect, Me.Factor, CornerStyle.Top)
                        g.FillPath(_Brush, _GraphicsPath)
                    End Using
                End Using
            End Using
        End Sub

        ''' <summary>
        ''' 绘制图标
        ''' </summary>
        ''' <param name="g"></param>
        ''' <param name="iconRectangle"></param>
        ''' <param name="icon"></param>
        ''' <remarks></remarks>
        Protected Overridable Sub DrawIcon(ByVal g As Graphics, ByVal iconRectangle As Rectangle, ByVal icon As Icon)
            g.InterpolationMode = InterpolationMode.HighQualityBicubic
            g.DrawIcon(icon, iconRectangle)
        End Sub

        ''' <summary>
        ''' 绘制标题文字
        ''' </summary>
        ''' <param name="g"></param>
        ''' <param name="textRectangle"></param>
        ''' <param name="text"></param>
        ''' <param name="font"></param>
        ''' <remarks></remarks>
        Protected Overridable Sub DrawCaptionText(ByVal g As Graphics, ByVal textRectangle As Rectangle, ByVal text As String, ByVal font As Font)
            TextRenderer.DrawText(g, text, font, textRectangle, Me.Theme.CaptionText, _
                                  TextFormatFlags.VerticalCenter Or _
                                  TextFormatFlags.Left Or _
                                  TextFormatFlags.SingleLine Or _
                                  TextFormatFlags.WordEllipsis)
        End Sub

        ''' <summary>
        ''' 绘制关闭按钮
        ''' </summary>
        ''' <param name="g"></param>
        ''' <param name="rect"></param>
        ''' <param name="state"></param>
        ''' <param name="active"></param>
        ''' <param name="minimizeBox"></param>
        ''' <param name="maximizeBox"></param>
        ''' <remarks></remarks>
        Protected Overridable Sub DrawCloseBox(ByVal g As Graphics, _
                                               ByVal rect As Rectangle, _
                                               ByVal state As ControlBoxState, _
                                               ByVal active As Boolean, _
                                               ByVal minimizeBox As Boolean, _
                                               ByVal maximizeBox As Boolean)

            Dim _Color As Color = Me.Theme.ControlBoxActive
            If state = ControlBoxState.Pressed Then
                _Color = Me.Theme.ControlCloseBoxPressed
            ElseIf state = ControlBoxState.Hover Then
                _Color = Me.Theme.ControlCloseBoxHover
            Else
                _Color = If(active, Me.Theme.ControlBoxActive, Me.Theme.ControlBoxDeactive)
            End If

            Dim _Style As CornerStyle = If(minimizeBox OrElse maximizeBox, CornerStyle.RightBottom, CornerStyle.Bottom)
            Using _OptimizeGraphics As New OptimizeGraphics(g)
                '背景
                Using _GraphicsPath As GraphicsPath = GraphicsPathHelper.CreateFlatRectanglePath(rect, 3, _Style)
                    g.FillPath(New SolidBrush(_Color), _GraphicsPath)
                    Using _Pen As New Pen(Me.Theme.Border)
                        g.DrawPath(_Pen, _GraphicsPath)
                    End Using
                End Using

                rect.Inflate(-1, -1)
                Using _GraphicsPath As GraphicsPath = GraphicsPathHelper.CreateFlatRectanglePath(rect, 3, _Style)
                    Using _Pen As New Pen(Me.Theme.InnerBorder)
                        g.DrawPath(_Pen, _GraphicsPath)
                    End Using
                End Using

                Using _GraphicsPath As GraphicsPath = GraphicsPathHelper.CreateClosePath(rect, 5, 2)
                    g.FillPath(Brushes.White, _GraphicsPath)
                    Using _Pen As New Pen(Color.Black)
                        g.DrawPath(_Pen, _GraphicsPath)
                    End Using
                End Using
            End Using
        End Sub

        ''' <summary>
        ''' 绘制最大化按钮
        ''' </summary>
        ''' <param name="g"></param>
        ''' <param name="rect"></param>
        ''' <param name="state"></param>
        ''' <param name="active"></param>
        ''' <param name="minimizeBox"></param>
        ''' <param name="maximize"></param>
        ''' <remarks></remarks>
        Protected Overridable Sub DrawMaximizeBox(ByVal g As Graphics, _
                                                  ByVal rect As Rectangle, _
                                                  ByVal state As ControlBoxState, _
                                                  ByVal active As Boolean, _
                                                  ByVal minimizeBox As Boolean, _
                                                  ByVal maximize As Boolean)

            Dim _Color As Color = Me.Theme.ControlBoxActive
            If state = ControlBoxState.Pressed Then
                _Color = Me.Theme.ControlBoxPressed
            ElseIf state = ControlBoxState.Hover Then
                _Color = Me.Theme.ControlBoxHover
            Else
                _Color = If(active, Me.Theme.ControlBoxActive, Me.Theme.ControlBoxDeactive)
            End If

            Dim _Style As CornerStyle = If(minimizeBox, CornerStyle.None, CornerStyle.LeftBottom)
            Using _OptimizeGraphics As New OptimizeGraphics(g)
                '背景
                Using _GraphicsPath As GraphicsPath = GraphicsPathHelper.CreateFlatRectanglePath(rect, 3, _Style)
                    g.FillPath(New SolidBrush(_Color), _GraphicsPath)
                    Using _Pen As New Pen(Me.Theme.Border)
                        g.DrawPath(_Pen, _GraphicsPath)
                    End Using
                End Using

                rect.Inflate(-1, -1)
                Using _GraphicsPath As GraphicsPath = GraphicsPathHelper.CreateFlatRectanglePath(rect, 3, _Style)
                    Using _Pen As New Pen(Me.Theme.InnerBorder)
                        g.DrawPath(_Pen, _GraphicsPath)
                    End Using
                End Using

                Using _GraphicsPath As GraphicsPath = GraphicsPathHelper.CreateMaximizePath(rect, 3, maximize, 2)
                    g.FillPath(Brushes.White, _GraphicsPath)
                    Using _Pen As New Pen(Color.Black)
                        g.DrawPath(_Pen, _GraphicsPath)
                    End Using
                End Using

            End Using
        End Sub

        ''' <summary>
        ''' 绘制最小化按钮
        ''' </summary>
        ''' <param name="g"></param>
        ''' <param name="rect"></param>
        ''' <param name="state"></param>
        ''' <param name="active"></param>
        ''' <remarks></remarks>
        Protected Overridable Sub DrawMinimizeBox(ByVal g As Graphics, _
                                                  ByVal rect As Rectangle, _
                                                  ByVal state As ControlBoxState, _
                                                  ByVal active As Boolean)
            Dim _Color As Color = Me.Theme.ControlBoxActive
            If state = ControlBoxState.Pressed Then
                _Color = Me.Theme.ControlBoxPressed
            ElseIf state = ControlBoxState.Hover Then
                _Color = Me.Theme.ControlBoxHover
            Else
                _Color = If(active, Me.Theme.ControlBoxActive, Me.Theme.ControlBoxDeactive)
            End If

            Dim _Style As CornerStyle = CornerStyle.LeftBottom

            Using _OptimizeGraphics As New OptimizeGraphics(g)
                '背景
                Using _GraphicsPath As GraphicsPath = GraphicsPathHelper.CreateFlatRectanglePath(rect, 3, _Style)
                    g.FillPath(New SolidBrush(_Color), _GraphicsPath)
                    Using _Pen As New Pen(Me.Theme.Border)
                        g.DrawPath(_Pen, _GraphicsPath)
                    End Using
                End Using

                rect.Inflate(-1, -1)
                Using _GraphicsPath As GraphicsPath = GraphicsPathHelper.CreateFlatRectanglePath(rect, 3, _Style)
                    Using _Pen As New Pen(Me.Theme.InnerBorder)
                        g.DrawPath(_Pen, _GraphicsPath)
                    End Using
                End Using

                Using _GraphicsPath As GraphicsPath = GraphicsPathHelper.CreateMinimizePath(rect, 3, 2)
                    g.FillPath(Brushes.White, _GraphicsPath)
                    Using _Pen As New Pen(Color.Black)
                        g.DrawPath(_Pen, _GraphicsPath)
                    End Using
                End Using
            End Using
        End Sub

#End Region

#End Region

        Public Overrides Function CreateRegion(ByVal frm As FormBase) As System.Drawing.Region
            Dim _Rectangle As New Rectangle(Point.Empty, frm.Size)
            Using _GraphicsPath As GraphicsPath = GraphicsPathHelper.CreateFlatRectanglePath(_Rectangle, Me.Factor, CornerStyle.All)
                Return New Region(_GraphicsPath)
            End Using
        End Function

        Public Overrides Sub InitForm(ByVal frm As FormBase)
            frm.BackColor = Me.Theme.Back
        End Sub

        Protected Overrides Sub OnBackgroundRender(ByVal e As BackgroundRenderEventArgs)
            Dim _Graphics As Graphics = e.Graphics
            Dim _Rectangle As Rectangle = e.ClipRectangle
            Dim _Form As FormBase = e.Form

            Using _OptimizeGraphics As New OptimizeGraphics(_Graphics)
                Using _Brush As New LinearGradientBrush(_Rectangle, Color.Transparent, Color.Transparent, LinearGradientMode.Vertical)
                    Dim _Colors(1) As Color
                    _Colors(0) = Me.Theme.Back
                    _Colors(1) = RenderHelper.GetColor(Me.Theme.Back, 0, 35, 24, 9)
                    Dim _ColorBlend As New ColorBlend()
                    With _ColorBlend
                        .Positions = New Single() {0.0!, 1.0!}
                        .Colors = _Colors
                    End With
                    _Brush.InterpolationColors = _ColorBlend

                    Using _GraphicsPath As GraphicsPath = GraphicsPathHelper.CreateFlatRectanglePath(_Rectangle, Me.Factor, CornerStyle.All)
                        _Graphics.FillPath(_Brush, _GraphicsPath)
                    End Using
                End Using
            End Using
        End Sub

        Protected Overrides Sub OnBorderRender(ByVal e As BorderRenderEventArgs)
            Dim _Graphics As Graphics = e.Graphics
            Using _OptimizeGraphics As New OptimizeGraphics(_Graphics)
                Me.drawBorder(_Graphics, e.ClipRectangle, Me.Factor, CornerStyle.All)
            End Using
        End Sub

        Protected Overrides Sub OnCaptionRender(ByVal e As CaptionRenderEventArgs)
            Dim _Graphics As Graphics = e.Graphics
            Dim _ClipRectangle As Rectangle = e.ClipRectangle
            Dim _Form As FormBase = e.Form
            Dim _IconRectangle As Rectangle = _Form.IconRectangle
            Dim _TextRectangle As Rectangle = Rectangle.Empty

            Dim _CloseBox As Boolean = _Form.ControlBox
            Dim _MinimizeBox As Boolean = _Form.ControlBox And _Form.MinimizeBox
            Dim _MaximizeBox As Boolean = _Form.ControlBox And _Form.MaximizeBox

            Dim _TextWidthDec As Integer = 0

            If _CloseBox Then
                'textWidthDec += form.CloseBoxSize.Width + form.ControlBoxOffset.X;
                _TextWidthDec += _Form.CloseBoxSize.Width
            End If

            If _MaximizeBox Then
                'textWidthDec += form.MaximizeBoxSize.Width + form.ControlBoxSpace;
                _TextWidthDec += _Form.MaximizeBoxSize.Width
            End If

            If _MinimizeBox Then
                'textWidthDec += form.MinimizeBoxSize.Width + form.ControlBoxSpace;
                _TextWidthDec += _Form.MinimizeBoxSize.Width
            End If

            _TextRectangle = New Rectangle(_IconRectangle.Right + 3, _
                                           _Form.BorderWidth, _
                                           _ClipRectangle.Width - _IconRectangle.Right - _TextWidthDec - 6, _
                                           _ClipRectangle.Height - _Form.BorderWidth)

            Using _OptimizeGraphics As New OptimizeGraphics(_Graphics)
                Me.DrawCaptionBackground(_Graphics, _ClipRectangle, e.Active)

                If _Form.ShowIcon AndAlso _Form.Icon IsNot Nothing Then
                    Me.DrawIcon(_Graphics, _IconRectangle, _Form.Icon)
                End If
                If String.IsNullOrEmpty(_Form.Text) = False Then
                    Me.DrawCaptionText(_Graphics, _TextRectangle, _Form.Text, _Form.CaptionFont)
                End If
            End Using

        End Sub

        Protected Overrides Sub OnControlBoxRender(ByVal e As ControlBoxRenderEventArgs)
            Dim _Form As FormBase = e.Form
            Dim _Graphics As Graphics = e.Graphics
            Dim _Rectangle As Rectangle = e.ClipRectangle
            Dim _State As ControlBoxState = e.ControlBoxState
            Dim _Active As Boolean = e.Active

            Dim _MinimizeBox As Boolean = (_Form.ControlBox And _Form.MinimizeBox)
            Dim _MaximizeBox As Boolean = (_Form.ControlBox And _Form.MaximizeBox)

            Select Case e.ControlBoxStyle
                Case ControlBoxStyle.Close
                    Me.DrawCloseBox(_Graphics, _Rectangle, _State, _Active, _MinimizeBox, _MaximizeBox)
                    Exit Select
                Case ControlBoxStyle.Maximize
                    Me.DrawMaximizeBox(_Graphics, _Rectangle, _State, _Active, _MinimizeBox, (_Form.WindowState = FormWindowState.Maximized))
                    Exit Select
                Case ControlBoxStyle.Minimize
                    Me.DrawMinimizeBox(_Graphics, _Rectangle, _State, _Active)
                    Exit Select
            End Select
        End Sub

        Public Overrides Function ToString() As String
            Return "平角矩形"
        End Function

    End Class

End Namespace

